package com.projectplace.GUI;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.util.List;
import com.commonsware.cwac.merge.MergeAdapter;
import com.projectplace.R;
import com.projectplace.SingeltonPPClient;
import com.projectplace.GUI.Callback.ViewConversationsCallback;
import com.projectplace.GUI.adapter.Document_Container_Adapter;
import com.projectplace.GUI.adapter.Document_Document_Adapter;
import com.projectplace.api.DocumentsApi;
import com.projectplace.api.ProjectApi;
import com.projectplace.api.objectResponses.DocumentsApi.Containers;
import com.projectplace.api.objectResponses.DocumentsApi.DocumentContainerContent;
import com.projectplace.api.objectResponses.DocumentsApi.Documents;
import com.projectplace.api.objectResponses.ProjectApi.ProjectTools;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class DocumentsContainerContent extends ListActivity{
	
	
	@SuppressWarnings("unused")
	private final String LOG_TAG ="Conversations";
	private ListActivity activity;
	@SuppressWarnings("unused")
	private String previusContainerID;
	private MergeAdapter mergeAdapter;
	private String idOfProject;
	private final String identifier="projectID";
	private String projectContainerID;
	private List<Containers> folders;
	private List<Documents> documents;
	private static Handler hand;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);	
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		hand = new Handler();
		setContentView(R.layout.list);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
		
		activity = this;
		idOfProject = getIntent().getStringExtra(identifier);
		
		ProjectApi.getProjectTools(SingeltonPPClient.getInstance(), hand, idOfProject, new com.projectplace.api.Callback.Callback.ResponceListenerProjectTools() {
			
			public void SERVER_ERROR(Error err) {
				// TODO Auto-generated method stub
				
			}
			
			public void BAD_TOKEN_ERROR(Error err) {
				// TODO Auto-generated method stub
				
			}
			public void onReceive(ProjectTools projecttools) {
				projectContainerID = projecttools.getDocuments_id();
				if(projectContainerID != null)
					onFolderClick(projectContainerID,0);
				else
					Toast.makeText(activity, "Something went wrong please try again", Toast.LENGTH_LONG).show();
			}
		});
		
	}
	
	public void onClick(View v){
		finish(); 
	}
	
	private void forward(String id,int position){	
		previusContainerID = id;
		onFolderClick(id,position);    
	}
	
	private void documentClicked(String id,int position){
		if(id != null){
			Intent intent = new Intent().setClass(this, DocumentsView.class);
			intent.putExtra("size",documents.get(position).getSize());
			intent.putExtra("type",documents.get(position).getType());
			intent.putExtra("name",documents.get(position).getName());
			intent.putExtra("time", documents.get(position).getModified_time());
			intent.putExtra("version", documents.get(position).getVersion());
			intent.putExtra("id", id);
			startActivity(intent);
		}
		else
			Toast.makeText(activity, "Something went very wrong please try again", Toast.LENGTH_LONG).show();
	}
	
	private void onFolderClick(String id,int position){
		mergeAdapter =  new MergeAdapter();
		DocumentsApi.getDocumentContainerContents(SingeltonPPClient.getInstance(), hand, id, new com.projectplace.api.Callback.Callback.ResponceListenerDocumentContainerContents() {
			
			public void SERVER_ERROR(Error err) {
				// TODO Auto-generated method stub
				
			} 
			
			public void BAD_TOKEN_ERROR(Error err) { 
				// TODO Auto-generated method stub
				
			}
			
			public void onReceive(DocumentContainerContent container) {
				// TODO Auto-generated method stub
				folders = container.getContainers();
				mergeAdapter.addAdapter(new Document_Container_Adapter(activity, folders,new ViewConversationsCallback() {
					
					public void onViewClick(String id,String name,int position) {
						// TODO Auto-generated method stub
						forward(id,position);
					}
				}));
				documents = container.getDocuments();
				mergeAdapter.addAdapter(new Document_Document_Adapter(activity, documents, new ViewConversationsCallback() {
					
					public void onViewClick(String id,String name, int position) {
						// TODO Auto-generated method stub
						documentClicked(id,position);
					}
				}));
				activity.setListAdapter(mergeAdapter);
			}
		});
	}
}
